import VueRouter from 'vue-router'

import Login from './components/Login.vue'
import NotFound from './components/NotFound.vue'
import Home from './components/home.vue'
import Welcome from './components/Welcome.vue'
import ClassView from './components/goodsManager/ClassView.vue'
import GoodsView from './components/goodsManager/GoodsView.vue'
import SearchView from './components/searchManager/SearchView.vue'
import OrderView from './components/orderManager/OrderView.vue'
import WaitOrderView from './components/orderManager/WaitOrderView.vue'
import UserView from './components/userManager/UserView.vue'
import OrderStatistics from './components/statistics/OrderStatistics.vue'
import GoodsStatistics from './components/statistics/GoodsStatistics.vue'
import GoodsPriceChart from './components/statistics/GoodsPriceChart.vue'

let routes = [
    {
        path: '/login',
        component: Login,
        name: '',
        hidden: true
    },
    {
        path: '/404',
        component: NotFound,
        name: '',
        hidden: true
    },
    {
        path: '/',
        component: Home,
        name: '',
        hidden: true,
        children: [
            { path: '/welcome', component: Welcome, name: '欢迎页面'}
        ]
    },
    {
        path: '/',
        component: Home,
        name: '信息统计',
        iconCls: 'el-icon-info',
        children: [
            { path: '/goodsPriceChart', component: GoodsPriceChart, name: '商品价格走势' },
            { path: '/orderStatistics', component: OrderStatistics, name: '订单统计' },
            { path: '/goodsStatistics', component: GoodsStatistics, name: '商品统计' }
        ]
    },
    {
        path: '/',
        component: Home,
        name: '商品管理',
        iconCls: 'el-icon-tickets',
        children: [
            { path: '/classView', component: ClassView, name: '分类查看' },
            { path: '/goodsView', component: GoodsView, name: '商品查看' }
        ]
    },
    {
        path: '/',
        component: Home,
        name: '搜索管理',
        iconCls: 'el-icon-search',
        children: [
            { path: '/searchView', component: SearchView, name: '搜索查看' }
        ]
    },
    {
        path: '/',
        component: Home,
        name: '订单管理',
        iconCls: 'el-icon-document',
        children: [
            { path: '/orderView', component: OrderView, name: '订单查看' },
            { path: '/waitOrderView', component: WaitOrderView, name: '待比对订单查看' }
        ]
    },
    {
        path: '/',
        component: Home,
        name: '用户管理',
        iconCls: 'el-icon-share',
        children: [
            { path: '/userView', component: UserView, name: '用户查看' }
        ]
    },

];

// 设置路由过滤器
const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    if(to.path == '/login') {
        sessionStorage.removeItem('token');
    }
    let token = sessionStorage.getItem('token');
    if (!token && to.path != '/login') {
        next({ path: '/login' });
    }
    else {
        next();
    }
});

export default router;
